package persistency.writer;

import java.io.File;
import java.io.IOException;
import java.util.Formatter;

public abstract class Writer {

	private File file;
	private Formatter output = null;
	
	/**
	 * @param filePath The file that will be used to store the data
	 * @param header The header that will be written in data file
	 */
	public Writer(String filePath, String header) {
		file = new File(filePath);
		try {
			file.createNewFile();
			output = new Formatter(file);
			write(header);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Closes the Formatter
	 */
	public void close(){
		if (output != null) {
			output.close();
		}
	}
	/**
	 * Writes a writable to a text file
	 * @param writable The writable to write to the text file
	 */
	public void write(Writable writable){
		output.format("%s", writable.toDataString());
	}
	
	/**
	 * Writes a String to a text file
	 * @param data
	 */
	public void write(String data){
		output.format("%s", data);
	}
}
